<template>
<div>
<template>

<!-- 添加老师隐藏端口 -->
<el-dialog title="添加老师" :visible.sync="dialogFormVisible">
  <el-form :model="teacher">
    <el-form-item label="老师姓名" label-width="120px">
      <el-input v-model="teacher.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="老师密码" label-width="120px">
      <el-input v-model="teacher.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="老师工号" label-width="120px">
      <el-input  v-model="teacher.tNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="老师邮箱" label-width="120px">
      <el-input v-model="teacher.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddTeacher()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddTeacher()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改老师隐藏端口 -->
    <el-dialog title="修改老师" :visible.sync="updateDialogFormVisible">
  <el-form :model="uTeacher">
    <el-form-item label="老师姓名" label-width="120px">
      <el-input v-model="uTeacher.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="老师密码" label-width="120px">
      <el-input v-model="uTeacher.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="老师工号" label-width="120px">
      <el-input  v-model="uTeacher.tNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="老师邮箱" label-width="120px">
      <el-input v-model="uTeacher.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdateTeacher()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedTeacher()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加老师按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addTeacher()">增加老师</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="teacherDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>

     <!-- 导出Excel -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="exportExcel()">导出Excel</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="teachers" stripe border height="300" id="mytable">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="user.username" sortable=true label="昵称"/>
    <el-table-column prop="tNo" sortable=true label="老师号码" />
    <el-table-column prop="user.email" sortable=true label="角色号码" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updateTeacher(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deleteTeacher(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="teacherDTO.capacity"
    :current-page.sync="teacherDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '老师',
      // 表格映射对象
      teachers: [],
      // 修改的对象
      uTeacher: {
        userJson: {
          '@type': 'com.nciae.po.User'
        },
        username: '',
        password: '',
        tNo: '',
        email: ''
      },
      // 表格查询对象
      teacherDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加老师弹出控制
      dialogFormVisible: false,
      // 修改老师弹出控制
      updateDialogFormVisible: false,
      // 添加老师实体
      teacher: {
        username: '',
        password: '',
        email: '',
        userJson: {
          '@type': 'com.nciae.po.User',
            username: '',
            password: '',
            email: ''
        }
      },
      // 老师列表总数
      count: 0
    }
  },
  // 页面初始化函数 
  created () {
    this.$http({
        url: "/teacher",
        params: {
            username: this.teacherDTO.username
        }
    }).then(response => {
       this.teachers = response.data
    })
  },
  methods: {


    // 改变当前页
    changeCurrent() {
      findAll()
    },


    // 添加老师
     addTeacher() {
       this.dialogFormVisible=true;
     },

     // 修改老师信息
     updateTeacher(data) {
       this.uTeacher.id = data.user.id
       this.uTeacher.username=data.user.username
       this.uTeacher.password=data.user.password
       this.uTeacher.email=data.user.email
       this.uTeacher.tNo=data.tNo
       this.updateDialogFormVisible=true
     },

     // 删除老师信息
     deleteTeacher(id) {
        this.$http.delete('/teacher/' + id).then(response => {
          this.$message({
          message: '删除老师成功',
          type: 'success'
          });
          this.findAll()
        })
     },

     // 添加老师信息
     ensureAddTeacher() {
      this.teacher.userJson.username=this.teacher.username
      this.teacher.userJson.password=this.teacher.password
      this.teacher.userJson.email=this.teacher.email
      this.teacher.userJson = JSON.stringify(this.teacher.userJson)
      this.$http.post("/teacher", this.teacher).then(response => {
        this.$message({
          message: '添加老师成功',
          type: 'success'
        });
        this.clearTeacher()
        this.findAll()
      })
    },


    // 取消添加老师
    cancelAddTeacher() {
      this.clearTeacher()
    },

    // 修改老师信息
    ensureUpdateTeacher() {

      this.uTeacher.userJson.username=this.uTeacher.username
      this.uTeacher.userJson.password=this.uTeacher.password
      this.uTeacher.userJson.email=this.uTeacher.email
      this.uTeacher.userJson = JSON.stringify(this.uTeacher.userJson)
      this.$http.put('/teacher', this.uTeacher).then(response => {
        this.$message({
          message: '修改老师成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },

    // 取消修改老师
    cancelUpdatedTeacher() {
      this.updateDialogFormVisible=false
    },


    // 搜索对象，并映射
    search() {
      this.findAll()
    },


    // 查询所有，并映射
    findAll() {
      this.$http({
          url: "/teacher",
          params: {
              username: this.teacherDTO.username
          }
      }).then(response => {
        this.teachers = response.data
      })
    },


    // 清除老师，并关闭窗口
    clearTeacher() {
        this.teacher.username=''
        this.teacher.password=''
        this.teacher.roleId=''
        this.teacher.teacherNo=''
        this.teacher.userJson = {'@type': 'com.nciae.po.User'}
        this.dialogFormVisible=false
     },


    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出老师信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }

  }
}
</script>
<style>

</style>